package com.william.vloadingviewtool;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import static android.content.ContentValues.TAG;

/**
 * Created by william on 2017/6/21.
 */

public class LoadingViewTool {

    //目标view缓存
    private View originalView;
    //当前展示中的view
    private View currentView;
    //view之间的转换器
//    private View targetView;

    private ViewGroup parent;
    private FrameLayout frameLayout;
    private int viewIndex;
    private ViewGroup.LayoutParams params;
    private boolean isNeedOver;

    /**
     * 初始化方法
     * @param originalView 准备要替换的View；
     * @param isNeedOver true:覆盖  false:替换;
     * 说明：推荐一般情况下传false用替换，这样效率更高，页面负荷小；
     *      如果页面数据较多，可传true采用覆盖，这样页面的切换会更加平顺。
     */
    public LoadingViewTool(View originalView, boolean isNeedOver) {

        //缓存View
        this.originalView = originalView;
        this.currentView = originalView;

        this.isNeedOver = isNeedOver;

        params = originalView.getLayoutParams();
        if (originalView.getParent() != null) {
            parent = (ViewGroup) originalView.getParent();
        } else {
            //没有父级容器，就把Window对象中的内容层作为父级
            parent = (ViewGroup) originalView.getRootView().findViewById(android.R.id.content);
        }
        //遍历父级容器下所有child，确定目标View在父级中的序号
        for (int i = 0, count = parent.getChildCount(); i<count; i++) {
            if (originalView == parent.getChildAt(i)) {
                viewIndex = i;
                break;
            }
        }

        if(this.isNeedOver) {
            //用浮动布局框架替换原View
            frameLayout = new FrameLayout(originalView.getContext());
            parent.removeView(originalView);
            parent.addView(frameLayout, params);

            //设置浮层适应浮动布局的大小，并用浮层和缓存的view填充浮动框架
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            frameLayout.addView(originalView, params);
            //缓存View
//            this.targetView = frameLayout;
        } else {
//            this.targetView = originalView;
        }
    }

    /**
     * 获取当前正在展示的View
     * @return
     */
    public final View getCurrentLayout() {
        return currentView;
    }

    /**
     * 恢复展示正常的View
     */
    public final void restoreView() {
        showLayout(this.originalView);
    }

    /**
     * 展示新View
     * @param view 需要被展示的view
     */
    public final void showLayout(View view) {

        this.currentView = view;
        /*
            从【需要被展示的View】的父容器中把【需要被展示的View】移除，否则这个view将无法被add，
         并且会运行时报错：view已有父级ViewGroup */
        ViewGroup group = (ViewGroup) view.getParent();
        if (!view.equals(this.originalView) && group != null) {
            group.removeView(view);
        }

        //这里才是真正的替换或覆盖
        if(this.isNeedOver){

            int count = this.frameLayout.getChildCount();
            if(view.equals(this.originalView)) {
                //如果是覆盖，则移除浮层
                if(count==2) {
                    this.frameLayout.removeViewAt(1);
                }
            } else {
                if (frameLayout.getChildCount()>1){
                    frameLayout.removeViewAt(1);
                }
                frameLayout.addView(view, params);
            }

        }else {
            this.parent.removeViewAt(viewIndex);
            this.parent.addView(view, viewIndex, params);
        }
//        this.targetView = view;
    }
}
